// routes/studentRoutes.js
const express = require('express');
const router = express.Router();
const StudentInfo = require('../models/getStudentInfo');  // 引入正确的StudentInfo模型

// 获取所有学生信息的GET路由
router.get('/getAllStudents', async (req, res) => {
  try {
    // 调用getAllStudents方法来获取所有学生数据
    const students = await StudentInfo.getAllStudents();
    
    if (students && students.length > 0) {
      res.status(200).json(students);  // 返回200和所有学生信息
    } else {
      res.status(404).json({ message: 'No students found' });  // 如果没有学生记录
    }
  } catch (error) {
    console.error('Error fetching students:', error);
    res.status(500).json({ message: 'Internal Server Error' });  // 如果发生错误
  }
});

// 根据id获取学生信息的GET路由
router.get('/getStudentById/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const student = await StudentInfo.getStudentById(id);
    
    res.status(200).json({
      success: true,
      data: student
    });
  } catch (error) {
    console.error('Error fetching student by id:', error);
    res.status(500).json({
      success: false,
      message: error.message || '获取学生信息失败'
    });
  }
});

module.exports = router;
